//
// Created by Joey on 2019/12/22.
//
#include"personnelManager.h"
#include "../../other/TerminalUtils.h"
#include "../../libs/lib_json/json/json.h"


PersonnelManager::PersonnelManager() {
    this->m_EmpNum = 0;
    this->m_EmpArray = nullptr;
    this->read();
}

void PersonnelManager::showMenu() {
    cout << "********************************************" << endl;
    cout << "*********  欢迎使用员工管理系统！ **********" << endl;
    cout << "*************  1.增加员工信息  *************" << endl;
    cout << "*************  2.显示员工信息  *************" << endl;
    cout << "*************  3.删除离职员工  *************" << endl;
    cout << "*************  4.修改员工信息  *************" << endl;
    cout << "*************  5.查找员工信息  *************" << endl;
    cout << "*************  6.按照编号排序  *************" << endl;
    cout << "*************  7.清空所有文档  *************" << endl;
    cout << "*************  0.退出管理程序  *************" << endl;
    cout << "********************************************" << endl;
    cout << endl;
}

void PersonnelManager::addEmp() {
    cout << "***** 1.添加员工 *****" << endl;

    int addNum = stoi(tipsAndInput("请输入添加员工数量："));
    if (addNum <= 0) {
        cout << "输入有误" << endl;
        return;
    }

    // 计算需要的空间大小
    int newSize = this->m_EmpNum + addNum;
    // 开辟新空间
    Personnel **newSpace = new Personnel *[newSize];
    // 将原来数组内容存到新数组空间中
    if (this->m_EmpArray != nullptr) {
        for (int i = 0; i < this->m_EmpNum; ++i) {
            newSpace[i] = this->m_EmpArray[i];
        }
    }
    // 输入新数据
    for (int i = 0; i < addNum; ++i) {
        int id = stoi(tipsAndInput("请输入第 " + to_string(i + 1) + " 个新员工编号："));
        string name = tipsAndInput("请输入第 " + to_string(i + 1) + " 个新员工姓名：");
        int dSelected = stoi(tipsAndInput("请选择该员工的岗位：\n1.普通员工\n2.经理\n3.老板"));

        Personnel *personnel = nullptr;
        switch (dSelected) {
            case 1:
                personnel = new Employee(id, name, 1);
                break;
            case 2:
                personnel = new Manager(id, name, 2);
                break;
            case 3:
                personnel = new Boss(id, name, 3);
                break;
            default:
                break;
        }

        newSpace[this->m_EmpNum + i] = personnel;
    }
    // 释放原有空间
    delete[] this->m_EmpArray;
    // 更该新的空间指向
    this->m_EmpArray = newSpace;

    this->m_EmpNum = newSize;

    cout << "成功添加 " << addNum << " 名新员工" << endl;

    this->save();
}

void PersonnelManager::save() {
    Json::Value root;
    Json::Value arrayObj;
    Json::Value item;

    for (int i = 0; i < this->m_EmpNum; ++i) {
        item[KEY_OF_PERSONNEL_ID] = m_EmpArray[i]->m_Id;
        item[KEY_OF_PERSONNEL_NAME] = m_EmpArray[i]->m_Name;
        item[KEY_OF_PERSONNEL_DEPTID] = m_EmpArray[i]->m_DeptId;

        arrayObj.append(item);
    }

    root[KEY_OF_PERSONNEL_ARRAY] = arrayObj;

    root.toStyledString();
    string out = root.toStyledString();

    ofstream ofs(FILENAME, ios::out);
    ofs << out;
    ofs.close();
}

void PersonnelManager::read() {
    ifstream ifs(FILENAME, ios::in);
    if (!ifs.is_open()) {
		return;
	}

    Json::CharReaderBuilder reader;
    Json::Value root;
    JSONCPP_STRING errs;
    
    Json::parseFromStream(reader, ifs, &root, &errs);
    const Json::Value arrayObj = root[KEY_OF_PERSONNEL_ARRAY];
    Personnel ** loadPersonnels = new Personnel*[arrayObj.size()];
    for (unsigned int i = 0; i < arrayObj.size(); i++) {
        if (!arrayObj[i].isMember(KEY_OF_PERSONNEL_ID)) {
            continue;
        }
        int id = arrayObj[i][KEY_OF_PERSONNEL_ID].asInt();
        string name = arrayObj[i][KEY_OF_PERSONNEL_NAME].asString();
        int deptId = arrayObj[i][KEY_OF_PERSONNEL_DEPTID].asInt();

        Personnel *personnel = nullptr;
        //根据不同的部门Id创建不同对象
        if (deptId == 1) {// 1普通员工
            personnel = new Employee(id, name, deptId);
        } else if (deptId == 2) {//2经理
            personnel = new Manager(id, name, deptId);
        } else if (deptId == 3) {//总裁
            personnel = new Boss(id, name, deptId);
        } else {
            continue;
        }
        //存放在数组中
        loadPersonnels[i] = personnel;
    }
    this->m_EmpArray = loadPersonnels;
    this->m_EmpNum = arrayObj.size();

    ifs.close();
}

void PersonnelManager::showEmps() {
    if (this->m_EmpNum == 0) {
        cout << "当前记录为空" << endl;
    }
    
    for (int i = 0; i < this->m_EmpNum; ++i) {
        this->m_EmpArray[i]->showInfo();
    }
}

void PersonnelManager::existSystem() {
    cout << "***** 欢迎下次使用 *****" << endl;
    exit(0);
}

PersonnelManager::~PersonnelManager() {
    if (this->m_EmpArray != nullptr) {
        delete[] this->m_EmpArray;
    }
}